 <template>
  <div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
    <el-button type="danger" plain size="small" @click="batchDel">批量删除</el-button>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="角色名">
      </el-table-column>
      <el-table-column
        prop="role_en"
        label="角色标识">
      </el-table-column>
      <el-table-column
        prop="description"
        label="角色描述">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="角色名" :label-width="formLabelWidth" prop="role_name">
          <el-input v-model="form.role_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色标识" :label-width="formLabelWidth" prop="role_en">
          <el-input v-model="form.role_en" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth" prop="description">
          <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDateRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {addOrUpdate, batchDelete, getOneRole, removeRole, rolePage} from "@/api/role";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      multipleSelection: [],
      currentPage: 1, //当前页
      pageSizes: [5, 10, 15, 20],
      pageSize: 5, //每页条数
      total: 0,
      rules: {
        role_name: [
          {required: true, message: '请输入角色名称', trigger: 'blur'},
        ],
        role_en: [
          {required: true, message: '请输入角色标识', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        role_name: '',
        role_en: '',
        description: ''
      }
    }
  },
  methods: {
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      getOneRole(row.id).then((res) => {
        this.form = res.data.role
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        removeRole(id).then(res => {
          this.$notify.success(res.message)
          this.rolePageData()
        })
      })
    },
    // 复选 参数  val  表示当前选中行的数据
    handleSelectionChange(val) {
      this.multipleSelection = val.map(row => row.id)
    },
    // 批量删除
    batchDel() {
      if (this.multipleSelection.length == 0) {
        this.$notify.warning('请至少选择一条数据')
        return
      }
      this.$confirm('删除选中数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        batchDelete({ids: this.multipleSelection}).then((res) => {
          this.$notify.success(res.message)
          this.rolePageData()
        })
      })
    },
    // 新增
    add() {
      this.title = '新增'
      this.dialogFormVisible = true
    },
    addOrUpDateRole() {
      addOrUpdate(this.form).then((res) => {
        this.$notify.success(res.message)
        this.dialogFormVisible = false
        this.rolePageData()
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.rolePageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.rolePageData()
    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {
        role_name: '',
        role_en: '',
        description: ''
      }
      this.$refs.dialogForm.clearValidate()
    },
    rolePageData() {
      rolePage({page: this.currentPage, size: this.pageSize})
        .then((res) => {
          this.tableData = res.tableData
          this.total = res.total
        })
    }
  },
  created() {
    this.rolePageData()
  }
}
</script>
<style scoped lang="scss">

</style>
